import Vue from 'vue'

const custom = require('./utils/custom').default

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'
import './assets/scss/common.scss'

import '@/styles/index.scss' // global css
import './assets/scss/theme.scss' // 主题

import App from './App'
import store from './store'
import router from './router'


import i18n from './lang' // internationalization
import './permission' // permission control
import './utils/error-log' // error log
import { message } from './utils/message';

import * as filters from './filters' // global filters
import BaiduMap from 'vue-baidu-map';
import { currency } from './utils/currency'
import moment from 'moment'

Vue.use(BaiduMap, {
  ak: '5wlqUjFj1QFedW2SlbUUiF4ybPSmiI0v' // 替换为实际 AK
});


Vue.filter("currency", currency);
// 自定义按钮权限指令
import permission from "@/directive/permission";
Vue.use(permission)

Vue.use(Element, {
    size: custom.storageGet('size') || 'small', // set element-ui default size
    i18n: (key, value) => i18n.t(key, value)
})
// 批量引入组件
import components from './components'
Vue.use(components)

// 添加实例属性
Object.assign(Vue.prototype, {
    define: require('./utils/define'), // 常量
    custom, // 公共方法
    formValidate: require('./utils/formValidate').default, // 表单验证
    $message: message,
    $moment: moment
})

// 全局注册过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

// 全局错误处理 - 捕获ResizeObserver循环通知错误
Vue.config.errorHandler = (err, vm, info) => {
  // 忽略ResizeObserver循环通知错误
  if (err.message && err.message.includes('ResizeObserver loop completed with undelivered notifications')) {
    console.warn('ResizeObserver循环通知已被忽略:', err.message)
    return
  }
  
  // 其他错误正常处理
  console.error('Vue全局错误:', err, info)
}

// 捕获未处理的Promise拒绝
window.addEventListener('unhandledrejection', event => {
  if (event.reason && event.reason.message && 
      event.reason.message.includes('ResizeObserver loop completed with undelivered notifications')) {
    console.warn('ResizeObserver循环通知Promise拒绝已被忽略:', event.reason.message)
    event.preventDefault()
    return
  }
  
  console.error('未处理的Promise拒绝:', event.reason)
})

// 捕获全局错误事件
window.addEventListener('error', event => {
  if (event.message && event.message.includes('ResizeObserver loop completed with undelivered notifications')) {
    console.warn('ResizeObserver循环通知错误已被忽略:', event.message)
    event.preventDefault()
    return
  }
  
  console.error('全局错误事件:', event.error)
})

const vm = new Vue({
    el: '#app',
    router,
    store,
    i18n,
    render: h => h(App)
})
export default vm
